package study.dq.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 计算一个时间范围内的时间点
 *
 * @author dongqun
 * @version 1.0.0
 * @ClassName Test2.java
 * @createTime 2020年11月19日 09:32:00
 */
public class DateRangePoint {
    /**
     * 获取当时时间前一小时的每隔十分钟的时间点
     *
     * @return 时间点集合
     */
    public static List<Date> getMinuteNodesOneHourBefore() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        List<Date> dates = new ArrayList<>();
        Date minute00, minute10, minute20, minute30, minute40, minute50;
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (currentHour == 0) {
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 1);
            String yesterday = simpleDateFormat.format(calendar.getTime());
            minute00 = dateFormat.parse(yesterday + " 23:00:00");
            minute10 = dateFormat.parse(yesterday + " 23:10:00");
            minute20 = dateFormat.parse(yesterday + " 23:20:00");
            minute30 = dateFormat.parse(yesterday + " 23:30:00");
            minute40 = dateFormat.parse(yesterday + " 23:40:00");
            minute50 = dateFormat.parse(yesterday + " 23:50:00");
        } else {
            String today = simpleDateFormat.format(calendar.getTime());
            minute00 = dateFormat.parse(today + " " + (currentHour - 1) + ":00:00");
            minute10 = dateFormat.parse(today + " " + (currentHour - 1) + ":10:00");
            minute20 = dateFormat.parse(today + " " + (currentHour - 1) + ":20:00");
            minute30 = dateFormat.parse(today + " " + (currentHour - 1) + ":30:00");
            minute40 = dateFormat.parse(today + " " + (currentHour - 1) + ":40:00");
            minute50 = dateFormat.parse(today + " " + (currentHour - 1) + ":50:00");
        }
        dates.add(minute00);
        dates.add(minute10);
        dates.add(minute20);
        dates.add(minute30);
        dates.add(minute40);
        dates.add(minute50);
        return dates;
    }

    /**
     * 当天小时整点时间点集合
     * <p>
     * 加入当前时间为 10:04:15.250 AM 则时间点集合为：0，1,...,10
     * </p>
     *
     * @return 小时整点时间点
     * @throws ParseException
     */
    public static Map<String, Date> getHourNodesWithinOneDay() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentDay = simpleDateFormat.format(calendar.getTime());
        SimpleDateFormat hourFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Date> dateMap = new HashMap<>();
        do {
            dateMap.put(String.valueOf(currentHour), hourFormat.parse(currentDay + " " + currentHour + ":00:00"));
            currentHour--;
        } while (currentHour >= 0);
        return dateMap;
    }

    /**
     * 获取最近7天
     *
     * @return yyyy-MM-dd -> date
     */
    public static Map<String, Date> getDayNodesWithinOneWeek() {
        Date current = new Date();
        return getDaysBetween(getDays(current,-7),current);
    }


    public static Map<String, Date> getDaysBetween(Date start,Date end) {
        Map<String, Date> dateMap = new HashMap<>();
        Calendar startC = Calendar.getInstance();
        startC.setTime(start);
        long startTime = startC.getTimeInMillis();
        Calendar endC = Calendar.getInstance();
        endC.setTime(end);
        long endTime = endC.getTimeInMillis();
        long oneDay = 1000 * 60 * 60 * 24L;
        long time = startTime;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            dateMap.put(df.format(d),d);
            time += oneDay;
        }
        return dateMap;
    }

    /**
     * 获取两个日期相差的天数
     *
     * @param start 开始时间
     * @param days   相差天数
     */
    public static Date getDays(Date start, int days) {
        long oneDay = 1000 * 60 * 60 * 24L;
        Calendar startC = Calendar.getInstance();
        startC.setTime(start);
        long startTime = startC.getTimeInMillis();
        return new Date(startTime + days * oneDay);
    }

    public static void main(String[] args) throws ParseException {
//        System.out.println(getHourNodesWithinOneDay());
//        System.out.println(getMinuteNodesOneHourBefore());
        System.out.println(getDayNodesWithinOneWeek());

    }
}
